package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class 日期转中文日期工具类 {

    private static final Map<Character, String> DIGIT_TO_CHINESE = new HashMap<>();
    private static final Map<Integer, String> SPECIAL_MONTHS = new HashMap<>();
    private static final Map<Integer, String> SPECIAL_DAYS = new HashMap<>();

    static {
        DIGIT_TO_CHINESE.put('0', "〇");
        DIGIT_TO_CHINESE.put('1', "一");
        DIGIT_TO_CHINESE.put('2', "二");
        DIGIT_TO_CHINESE.put('3', "三");
        DIGIT_TO_CHINESE.put('4', "四");
        DIGIT_TO_CHINESE.put('5', "五");
        DIGIT_TO_CHINESE.put('6', "六");
        DIGIT_TO_CHINESE.put('7', "七");
        DIGIT_TO_CHINESE.put('8', "八");
        DIGIT_TO_CHINESE.put('9', "九");

        SPECIAL_MONTHS.put(1, "一");
        SPECIAL_MONTHS.put(2, "二");
        SPECIAL_MONTHS.put(3, "三");
        SPECIAL_MONTHS.put(4, "四");
        SPECIAL_MONTHS.put(5, "五");
        SPECIAL_MONTHS.put(6, "六");
        SPECIAL_MONTHS.put(7, "七");
        SPECIAL_MONTHS.put(8, "八");
        SPECIAL_MONTHS.put(9, "九");
        SPECIAL_MONTHS.put(10, "十");
        SPECIAL_MONTHS.put(11, "十一");
        SPECIAL_MONTHS.put(12, "十二");

        SPECIAL_DAYS.put(1, "一");
        SPECIAL_DAYS.put(2, "二");
        SPECIAL_DAYS.put(3, "三");
        SPECIAL_DAYS.put(4, "四");
        SPECIAL_DAYS.put(5, "五");
        SPECIAL_DAYS.put(6, "六");
        SPECIAL_DAYS.put(7, "七");
        SPECIAL_DAYS.put(8, "八");
        SPECIAL_DAYS.put(9, "九");
        SPECIAL_DAYS.put(10, "十");
        SPECIAL_DAYS.put(11, "十一");
        SPECIAL_DAYS.put(12, "十二");
        SPECIAL_DAYS.put(13, "十三");
        SPECIAL_DAYS.put(14, "十四");
        SPECIAL_DAYS.put(15, "十五");
        SPECIAL_DAYS.put(16, "十六");
        SPECIAL_DAYS.put(17, "十七");
        SPECIAL_DAYS.put(18, "十八");
        SPECIAL_DAYS.put(19, "十九");
        SPECIAL_DAYS.put(20, "二十");
        SPECIAL_DAYS.put(21, "二十一");
        SPECIAL_DAYS.put(22, "二十二");
        SPECIAL_DAYS.put(23, "二十三");
        SPECIAL_DAYS.put(24, "二十四");
        SPECIAL_DAYS.put(25, "二十五");
        SPECIAL_DAYS.put(26, "二十六");
        SPECIAL_DAYS.put(27, "二十七");
        SPECIAL_DAYS.put(28, "二十八");
        SPECIAL_DAYS.put(29, "二十九");
        SPECIAL_DAYS.put(30, "三十");
        SPECIAL_DAYS.put(31, "三十一");
    }

    public static String dateToChinese(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);

            String year = chineseYear(date.getYear() + 1900);
            String month = chineseMonth(date.getMonth() + 1);
            String day = chineseDay(date.getDate());
//            String hour = chineseHour(date.getHours());
//            String minute = chineseMinute(date.getMinutes());
//            String second = chineseSecond(date.getSeconds());

            return year + "年" + month + "月" + day + "日 ";
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format", e);
        }
    }

    private static String chineseYear(int year) {
        StringBuilder sb = new StringBuilder();
        for (char c : String.valueOf(year).toCharArray()) {
            sb.append(DIGIT_TO_CHINESE.get(c));
        }
        return sb.toString();
    }

    private static String chineseMonth(int month) {
        if (SPECIAL_MONTHS.containsKey(month)) {
            return SPECIAL_MONTHS.get(month);
        } else {
            int tens = month / 10;
            int ones = month % 10;
            return DIGIT_TO_CHINESE.get((char) ('1' + (tens - 1))) + "十" + DIGIT_TO_CHINESE.get((char) ('0' + ones));
        }
    }

    private static String chineseDay(int day) {
        if (SPECIAL_DAYS.containsKey(day)) {
            return SPECIAL_DAYS.get(day);
        } else {
            int tens = day / 10;
            int ones = day % 10;
            return DIGIT_TO_CHINESE.get((char) ('1' + (tens - 2))) + "十" + DIGIT_TO_CHINESE.get((char) ('0' + ones));
        }
    }

    private static String chineseHour(int hour) {
        return DIGIT_TO_CHINESE.get((char) ('0' + (hour / 10))) + DIGIT_TO_CHINESE.get((char) ('0' + (hour % 10)));
    }

    private static String chineseMinute(int minute) {
        return DIGIT_TO_CHINESE.get((char) ('0' + (minute / 10))) + DIGIT_TO_CHINESE.get((char) ('0' + (minute % 10)));
    }

    private static String chineseSecond(int second) {
        return DIGIT_TO_CHINESE.get((char) ('0' + (second / 10))) + DIGIT_TO_CHINESE.get((char) ('0' + (second % 10)));
    }

    public static void main(String[] args) {
        String dateStr = "2024-12-31";
        System.out.println(dateToChinese(dateStr)); // 输出: 二零二三年四月一日 十五时三十十分二十秒
    }

}
